<!DOCTYPE html>
<html lang="ch">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        padding: 0;
        margin: 0;
        list-style: none;
      }

      .outer {
        margin: 140px auto 0 auto;
        display: flex;
        width: 640px;
      }

      .box {
        transform-style: preserve-3d;
        position: relative;
        transform-origin: 320px 180px -180px;
        height: 360px;
        transition: all .5s;
      }

      .box li {
        height: 360px;
        position: absolute;
        overflow: hidden;
      }

      .one {
        background: url(./1.jpg);
        background-size: cover;
      }

      .two {
        transform: translateZ(-360px) rotateX(-180deg);
        background: url(./2.jpg);
        background-size: cover;
      }

      .three {
        transform: translateY(180px) rotateX(-90deg) translateY(180px);
        background: url(./3.jpg);
        background-size: cover;
      }

      .four {
        transform: translateY(-180px) rotateX(90deg) translateY(-180px);
        background-size: cover;
        background: url(./4.jpg);
      }

      .btn {
        width: 640px;
        margin: 20px auto;
        display: flex;
      }

      .btn li {
        width: 20px;
        height: 20px;
        margin-right: 20px;
        background-color: red;
        border-radius: 50%;
        cursor: pointer;
      }

      .btn .active {
        background-color: black;
      }
    </style>
  </head>
  <body>
    <div class="outer"></div>

    <ul class="btn">
      <li></li>
      <li></li>
      <li></li>
      <li></li>
    </ul>

    <script>
      const outer = document.querySelector(".outer")
      const btn = document.querySelector(".btn")
      // 图片总宽
      let allWidth = 640
      // 要分成多少份
      let count = 20
      // 要渲染在页面上的元素
      let strDom = ""
      // 每块需要延迟的事件
      let time = 0.05
      // 亮起的index
      let index = 0
      for (let i = 0; i < count; i++) {
        strDom += `
        <ul class="box" style="transition-delay: ${time * i}s;width:${
          allWidth / count
        }px">
          <li class="one" style="background-position: -${
            i * (allWidth / count)
          }px;width:${allWidth / count}px"></li>
          <li class="two" style="background-position: -${
            i * (allWidth / count)
          }px;width:${allWidth / count}px"></li>
          <li class="three" style="background-position: -${
            i * (allWidth / count)
          }px;width:${allWidth / count}px"></li>
          <li class="four" style="background-position: -${
            i * (allWidth / count)
          }px;width:${allWidth / count}px"></li>
        </ul>
        `
      }
      // const boxes = document.querySelectorAll(".box")
      outer.innerHTML = strDom

      fun()

      for (let i = 0; i < btn.children.length; i++) {
        btn.children[i].onclick = () => {
          for (let j = 0; j < outer.children.length; j++) {
            outer.children[j].style.transform = `scaleY(1) rotateX(${
              90 * i
            }deg)`
          }
          console.log()
          index = i
          fun()
        }
      }

      function fun() {
        for (let i = 0; i < btn.children.length; i++) {
          btn.children[i].className = ""
        }
        btn.children[index].className = "active"
      }
    </script>
  </body>
</html>
